﻿DROP TRIGGER IF EXISTS  `pk_card_log`; 
CREATE  TRIGGER `pk_card_log` AFTER INSERT ON `open_card`
  FOR EACH ROW BEGIN
dECLARE pkf varchar(50) DEFAULT "";
sELECT pk_from INTO pkf FROM open_pk_log WHERE pk_from="card";
if pkf <> "card" then
  INSERT INTO open_pk_log SET pk_from = 'card', pk_id = NEW.card_id;
eLSE
   update open_pk_log SET pk_id = NEW.card_id WHERE pk_from = 'card';
end if;
end;